﻿using BaoVeThanhDia.GamePlay.Monsters;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;

namespace BaoVeThanhDia.GamePlay.Towers
{
    class BowTower : Tower
    {
        private float countDraw;// biến phụ đếm thời gian
        int isX2Ten;
        Random rand;
        public BowTower() { }
        public BowTower(Vector2 index, int level, int type)
        {
            sound = new Sound.CustomSoundEffect("sounds/effect/Tower/Mui_Ten");
            ValueBuff = 0;
            countDraw = 0;
            setIndex(index);
            setLevel(level, type);
            setelement();
            weapons = new List<Weapon>();
            isX2Ten = 0;
            rand = new Random();
        }
        public override void Load(ContentManager content)
        {
            sound.Load(content);
            scale = 128 / 100f;
            Image = content.Load<Texture2D>("images/towers/bow/" + Level.ToString() + (Type == 0 ? "" : Type.ToString()));
        }
        public override void setLevel(int level, int type)
        {
            Level = level;
            Type = type;
            Effect = "None";
        }
        public override void Draw(ContentManager cont, SpriteBatch sprite)
        {
            if (ValueBuff > 0)
            {
                ImageBuffed = cont.Load<Texture2D>("images/towers/soul/root" + (ValueBuff < 30 ? "" : (ValueBuff < 50 ? "1" : "2")));
                sprite.Draw(ImageBuffed, (Position + new Vector2(-13, -45)) * GamePage.Scale + GamePage.Dolech, new Rectangle(((int)countDraw / 5) % 6 * 100, 0, 100, 100), Color.White, 0f, Vector2.Zero, 1.5f * GamePage.Scale, SpriteEffects.None, 0f);
            }
            DrawWeapon(cont, sprite);
            if (Type == 2)
            {
                sprite.Draw(Image, (Position + new Vector2(0, -64)) * GamePage.Scale + GamePage.Dolech, new Rectangle(0, 0, 100, 130), Color.White, 0f, Vector2.Zero, scale * GamePage.Scale, SpriteEffects.None, 0f);
            }
            else
            {
                sprite.Draw(Image, (Position + new Vector2(0, -64)) * GamePage.Scale + GamePage.Dolech, new Rectangle(((int)(countDraw / 4) % 4) * 100, ((int)(countDraw / 4) / 4) * 130, 100, 130), Color.White, 0f, Vector2.Zero, scale * GamePage.Scale, SpriteEffects.None, 0f);
            }
            countDraw += Speed * GamePage.Speed / 2;
            if (countDraw >= 32) countDraw = 0;
        }
        public override float getPriceUpGrade(int level, int type)
        {
            return new BowTower(Vector2.Zero, level, type).Price;
        }
        public override void DrawPictureUpGrade(int level, int type, ContentManager cont, SpriteBatch sprite, Vector2 posi)
        {
            BowTower temp = new BowTower(Vector2.Zero, level, type);
            temp.Load(cont);
            temp.OptionDraw(sprite, posi + new Vector2(6, -20) * GamePage.Scale, GamePage.Dolech, 0.8f * GamePage.Scale, false);
        }
        public override void OptionDraw(SpriteBatch sprite, Vector2 posi, Vector2 dolech, float scale, bool isTouch)
        {
            if (Type == 2)
            {
                sprite.Draw(Image, posi + dolech, new Rectangle(0, 0, 100, 130), Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
            }
            else
            {
                sprite.Draw(Image, posi + dolech, new Rectangle(((int)(countDraw / 4) % 4) * 100, ((int)(countDraw / 4) / 4) * 130, 100, 130), Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);
            }
            if (isTouch)
            {
                countDraw += Speed;
                if (countDraw >= 32) countDraw = 0;
            }
        }
        public override void Ban()
        {
            if ((int)countDraw / 4 == 7 && (int)countDraw % 4 == 0)
            {
                #region xét nâng cấp bắn 2 mũi tên
                bool isBan2Lan = false;
                if (isX2Ten == 10 && rand.Next(10000) % 100 < 10) isBan2Lan = true;
                #endregion
                #region xét hiệu ứng nhân 3 sát thương
                bool isBanX3 = false;
                if (countEffect != 0 && rand.Next(10000) % 100 < countEffect) isBanX3 = true;
                #endregion
                int temp = 0;
                for (int i = 0; i < GamePage.monsters.Count; i++)
                {
                    if (Vector2.Distance(Position, GamePage.monsters[i].getPosition()) <= Range * 128 && GamePage.monsters[i].getCurHP() > 0)
                    {
                        #region có địch trong phạm vi thì xử
                        sound.Play();
                        if (Type == 0)
                        {
                            weapons.Add(new Weapon(1, PhysicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 25, Position + new Vector2(64, 0) * GamePage.Scale, GamePage.monsters[i].getPosition() + new Vector2(64, 64) * GamePage.Scale, i, Effect, 0, countEffect));
                            if (isBan2Lan) weapons.Add(new Weapon(1, PhysicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 25, Position + new Vector2(64, 0) * GamePage.Scale, GamePage.monsters[i].getPosition() + new Vector2(64, 64) * GamePage.Scale, i, Effect, 0, countEffect));
                            break;
                        }
                        else if (Type == 1)
                        {
                            #region bắn đa mục tiêu
                            if (temp < Level - 1)
                            {
                                temp++;
                                weapons.Add(new Weapon(1, PhysicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 25, Position + new Vector2(64, 0) * GamePage.Scale, GamePage.monsters[i].getPosition() + new Vector2(64, 64) * GamePage.Scale, i, Effect, 0, countEffect));
                                if (isBan2Lan) weapons.Add(new Weapon(1, PhysicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 25, Position + new Vector2(64, 0) * GamePage.Scale, GamePage.monsters[i].getPosition() + new Vector2(64, 64) * GamePage.Scale, i, Effect, 0, countEffect));
                            }
                            else break;
                            #endregion
                        }
                        else
                        {
                            #region bắn xác suất x3 sát thương
                            weapons.Add(new Weapon(1, PhysicDame * (isBanX3 ? 3 : 1) * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1) * (isBanX3 ? 3 : 1), 25, Position + new Vector2(64, 0) * GamePage.Scale, GamePage.monsters[i].getPosition() + new Vector2(64, 64) * GamePage.Scale, i, Effect, 0, countEffect));
                            if (isBan2Lan) weapons.Add(new Weapon(1, PhysicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), MagicDame * (ValueBuff != 0 ? ValueBuff / 100.0f : 1), 25, Position + new Vector2(64, 0) * GamePage.Scale, GamePage.monsters[i].getPosition() + new Vector2(64, 64) * GamePage.Scale, i, Effect, 0, countEffect));
                            break;
                            #endregion
                        }
                        #endregion
                    }
                }
            }
        }
        public override void setelement()
        {
            if (Level == 1)
            {
                Price = 100;
                PhysicDame = 25;
                MagicDame = 5;
                Speed = 0.8f;
                Range = 2f;
                countEffect = 0;
                PriceBought = 100;
            }
            else if (Level == 2)
            {
                Price = 50;
                PhysicDame = 29;
                MagicDame = 10;
                Speed = 0.9f;
                Range = 2.33f;
                countEffect = 0;
                PriceBought = 150;
            }
            else if (Level == 3)
            {
                Price = 75;
                Range = 2.66f;
                Speed = 1f;
                PriceBought = 225;
                if (Type == 1)
                {
                    PhysicDame = 30;
                    MagicDame = 10;
                    countEffect = 0;
                }
                else
                {
                    PhysicDame = 35;
                    MagicDame = 20;
                    countEffect = 15;
                }
            }
            else
            {
                Price = 75;
                Range = 2.66f;
                Speed = 1f;
                PriceBought = 300;
                if (Type == 1)
                {
                    PhysicDame = 35;
                    MagicDame = 10;
                    countEffect = 0;
                }
                else
                {
                    PhysicDame = 40;
                    MagicDame = 40;
                    countEffect = 30;
                }
            }
            getUpGradeValue();
        } // dành riêng cho tháp tên
        private void getUpGradeValue()
        {
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[12])
            {
                Speed += (Speed * 3) / 20;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[13])
            {
                MagicDame += (MagicDame * 3) / 20;
                PhysicDame += (PhysicDame * 3) / 20;
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[14])
            {
                Price -= (int)((Price * 3.0f) / 20);
                PriceBought -= (int)((PriceBought * 3.0f) / 20);
            }
            if (MapAndUpgrade.File_Upgrade_Star.ItemBought[15])
            {
                isX2Ten = 10;
            }
        }
    }
}
